﻿using System;
using System.Collections.Generic;
using System.Text;
using IceDog.DNL.CSharp.Model;

namespace IceDog.DNL.CSharp.Grammar.GenericParadigm
{
    /// <summary>
    /// 泛型
    /// </summary>
    /// <typeparam name="Param1"></typeparam>
    /// <typeparam name="Param2"></typeparam>
    public class ParamManager<Param1,Param2>
    {
        private readonly Param1 _p1;
        private readonly Param2 _p2;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        public ParamManager(Param1 p1, Param2 p2)
        {
            _p1 = p1;
            _p2 = p2;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Param1 GetFirstParam()
        {
            return _p1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Param2 GetSecondParam()
        {
            return _p2;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TParamType"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        public TParamType GetParam<TParamType>(TParamType param)
        {
            return param;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TParamType"></typeparam>
        /// <param name="param"></param>
        /// <remarks>直接奖泛型转为T是不能转换的 要先转Object</remarks>
        /// <returns></returns>
        public TParamType GetDefaultValue<TParamType>(TParamType param)
        {
            if (param == null)
            {
                return param;
                
            }
            var type = param.GetType();
            if (type == typeof(Person))
            {
                var p = param as Person;
                p.City = "CQ";
                p.Name = "GGG";
                p.Remark = "N/A";
                //转换方式1
                return (TParamType) (object) p;
            }
            if (type == typeof(City))
            {
                var p = param as City;
                p.CityId = 1;
                p.CityName = "GGG";
                p.Country = "China";
                //转换方式2
                return (TParamType)Convert.ChangeType(p,typeof(TParamType));
            }
            return param;
        }
    }
}
